<?php
/**
 * Created by PhpStorm.
 * User: M2
 * Date: 13-11-19
 * Time: 下午1:40
 */
class YlSqlContentTaglib {
    public function parse($content,$arr=array()) {
        if (stripos($content, 'yl:sqlcontent') !== false) {
            $matches = preg_parse($content, 'yl:sqlContent');
            foreach ($matches as $match) {
                $type = getParseOneAttr($match, 'type');
                $ellipsis = getParseOneAttr($match, 'ellipsis');
                $iscleartags = getParseOneAttr($match, 'isClearTags');
                $wordnum = getParseOneAttr($match, 'wordNum');
                $stripslashes = getParseOneAttr($match, 'stripslashes');
                $formatString = getParseOneAttr($match, 'formatString');
                $querystring = getParseOneAttr($match, 'querystring');
                if (!empty($arr)) {
                    if (!empty($type)) {
                        $ellipsis = isset($ellipsis) ? $ellipsis : '...';
                        $contents = $arr[$type];
                        if ($iscleartags === 'true') {
                        	$contents = html_entity_decode($contents);
                            $contents = strip_tags($contents);
                        }
                        if('true' ==$stripslashes){
                            //对所有字段进行处理
                            $contents = stripslashes($contents);
                        }
                        if (isset($wordnum)) {
                            $res = common('String')->msubstr ( $contents, 0, $wordnum, 'utf-8', true, $ellipsis );
                        } else {
                            $res = $contents;
                        }

                        if (strtolower($type) === 'updatetime' || strtolower($type) === 'inputtime') {
                            $formatString = $formatString?$formatString:'Y-m-d';
                            $res = date($formatString,$contents);
                        }
                    }
                } else {
                    if ($querystring) {
                        if(false === ($find = S('cache_sqlcontents_'.$querystring))){
                            $find = M()->query($querystring);
                            S('cache_sqlcontents_'.$querystring, $find, 600);
                        }
                        $findKey = array_keys($find[0]);
                        $res = $find[0][$findKey[0]];
                        if('true' == $stripslashes){
                            $res = stripslashes($res);
                        }
                    }
                }
                $content = str_replace($match, $res, $content);
            }
        }
        return $content;
    }
}